// const { compilation } = require("webpack")


class BannerWebpackPlugin {
  constructor(options = {}){
    this.options = options
  }
  apply(compiler) {
    compiler.hooks.emit.tapAsync("BannerWebpackPlugin", (compilation,callback) => {
      debugger;
      const extensions = ['css', 'js'];
      const assets = Object.keys(compilation.assets).filter((assetPath) => {
        const splitted = assetPath.split('.');
        const extension = splitted[splitted.length - 1];
        return extensions.includes(extension)
      })
      // 1. 获取即将输出的资源文件，compilation.assets
      // 2. 过滤只保留js和css 资源
      console.log(assets);
      console.log(compilation.assets);
      // 3. 遍历剩下资源添加上注释

      const prefix = `/*
      * Author: ${this.options.author}
      */
     `
      assets.forEach((asset) => {
        const source = compilation.assets[asset].source()
        // 评上注释
        const content = prefix+source;
        // 修改资源
        compilation.assets[asset] = {
          source(){
            return content;
          },
          // 资源大小
          size(){
            return content.length
          }
        }
      })
      callback();
    })
  }
}
module.exports = BannerWebpackPlugin